package com.gdut.simple.view.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.gdut.simple.R;
import com.gdut.simple.common.DateUtils;
import com.gdut.simple.data.entity.News;
import com.gdut.simple.view.adapter.base.AbsRecyclerAdapter;
import com.gdut.simple.view.adapter.base.AbsViewHolder;
import com.squareup.picasso.Picasso;

import java.util.List;

public class NewsAdapter extends AbsRecyclerAdapter<News> {

    public NewsAdapter(Context context, List<News> list) {
        super(context, list);
    }

    @Override
    protected AbsViewHolder createHolder(ViewGroup parent, int viewType) {
        return new ItemViewHolder(mInflater.inflate(R.layout.layout_news_item, parent, false));
    }

    @Override
    protected void showViewHolder(AbsViewHolder holder, int position) {
        final ItemViewHolder viewHolder = (ItemViewHolder) holder;
        viewHolder.title.setText(mData.get(position).getTitle());
        viewHolder.desc.setText(mData.get(position).getDescription());
        viewHolder.time.setText(DateUtils.formatDate(mData.get(position).getTime()));
        if (mData.get(position).getPicUrl() == null || mData.get(position).getPicUrl().isEmpty()) {
            viewHolder.image.setBackgroundResource(R.drawable.ic_load_error);
        } else {
            Picasso.with(mContext)
                    .load(mData.get(position).getPicUrl())
                    .error(R.drawable.ic_load_error)
                    .placeholder(R.drawable.ic_place_holder)
                    .config(Bitmap.Config.RGB_565)
                    .into(viewHolder.image);
        }
    }

    private static class ItemViewHolder extends AbsViewHolder {

        TextView title;
        TextView desc;
        TextView time;
        ImageView image;

        ItemViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.id_news_title);
            desc = (TextView) itemView.findViewById(R.id.id_news_desc);
            time = (TextView) itemView.findViewById(R.id.id_news_time);
            image = (ImageView) itemView.findViewById(R.id.id_news_image);
        }
    }

}
